/*
 * @Author: SamZhang samzhang@apjcorp.com
 * @Date: 2022-12-02 10:14:44
 * @LastEditors: SamZhang samzhang@apjcorp.com
 * @LastEditTime: 2023-02-16 15:14:05
 * @FilePath: /circulation-web/src/components/LoggingServices.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import EditableConfigList, {
  EditableConfigListStateProps,
  EditableConfigListDispatchProps,
  EditableConfigListOwnProps,
} from "./EditableConfigList";
import { connect } from "react-redux";
import ActionCreator from "../actions";
import { LoggingServicesData, LoggingServiceData } from "../interfaces";
import ServiceEditForm from "./ServiceEditForm";

const applocation_root = localStorage.getItem("MOBEBR_CONFIG_APPLICATION_ROOT");
/** Right panel for logging settings on the system configuration page.
    Shows a list of current logging settings and allows creating a new
    setting or editing or deleting an existing setting. */
export class LoggingServices extends EditableConfigList<
  LoggingServicesData,
  LoggingServiceData
> {
  EditForm = ServiceEditForm;
  listDataKey = "logging_services";
  itemTypeName = "logging service";
  urlBase = applocation_root + "/admin/web/config/logging/";
  identifierKey = "id";
  labelKey = "protocol";
}

function mapStateToProps(state, ownProps) {
  // fetchError = an error involving loading the list of logging services; formError = an error upon submission of the
  // create/edit form.
  return {
    data: state.editor.loggingServices && state.editor.loggingServices.data,
    responseBody:
      state.editor.loggingServices &&
      state.editor.loggingServices.successMessage,
    fetchError: state.editor.loggingServices.fetchError,
    formError: state.editor.loggingServices.formError,
    isFetching:
      state.editor.loggingServices.isFetching ||
      state.editor.loggingServices.isEditing,
  };
}

function mapDispatchToProps(dispatch, ownProps) {
  const actions = new ActionCreator(null, ownProps.csrfToken);
  return {
    fetchData: () => dispatch(actions.fetchLoggingServices()),
    editItem: (data: FormData) => dispatch(actions.editLoggingService(data)),
    deleteItem: (identifier: string | number) =>
      dispatch(actions.deleteLoggingService(identifier)),
  };
}

const ConnectedLoggingSettings = connect<
  EditableConfigListStateProps<LoggingServicesData>,
  EditableConfigListDispatchProps<LoggingServicesData>,
  EditableConfigListOwnProps
>(
  mapStateToProps,
  mapDispatchToProps
)(LoggingServices);

export default ConnectedLoggingSettings;
